package com.yc.cn.ycrecycleview.first;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yc.cn.ycrecycleview.R;
import com.yc.cn.ycrecycleviewlib.select.SelectRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by PC on 2017/12/13.
 * 作者：PC
 */

public class FirstActivity extends AppCompatActivity {


    private TextView tvSelect;
    private Button btnClear;
    private Button btnAll;
    private TextView tvSelectCount;
    private SelectRecyclerView selectView;
    private List<Student> lists = new ArrayList<>();
    private SelectFollowAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);


        selectView = (SelectRecyclerView) findViewById(R.id.select_view);
        tvSelect = (TextView) findViewById(R.id.tv_select);
        btnClear = (Button) findViewById(R.id.btn_clear);
        btnAll = (Button) findViewById(R.id.btn_all);
        tvSelectCount = (TextView) findViewById(R.id.tv_select_count);
        init();
    }

    private void init() {
        lists.clear();
        for(int i=0 ; i<30 ; i++){
            Student student = new Student("杨充"+i,false);
            lists.add(student);
        }

        selectView.setLayoutManager(new GridLayoutManager(this, 4));
        adapter = new SelectFollowAdapter(this, lists);
        selectView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnListItemClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onItemClick(View view, int position) {
                if (adapter.data != null && adapter.data.size() > 0) {
                    adapter.toggleSelected(position);
                }
                select();
            }
        });
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.clearSelected();
                select();
            }
        });
        btnAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.selectAll();
                select();
            }
        });
    }

    private void select(){
        int selectedCount = adapter.getSelectedCount();
        Integer[] selectedIndices = adapter.getSelectedIndices();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i=0 ; i<selectedIndices.length ; i++){
            stringBuffer.append(selectedIndices[i]);
            stringBuffer.append("，");
        }
        tvSelect.setText("选中的索引："+stringBuffer.toString());
        tvSelectCount.setText("选中的索引个数："+selectedCount);
    }

}
